package com.neusoft.yuejian_6.activity;

import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;

import com.neusoft.yuejian_6.R;
import com.neusoft.yuejian_6.adapter.SearchAdapter;
import com.neusoft.yuejian_6.adapter.TrainAdapter;
import com.neusoft.yuejian_6.javabean.Train;
import com.neusoft.yuejian_6.model.TrainModel;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class SearchTrainActivity extends Activity {

    private int train_id;
    private EditText search_train;
    private Button searchTrain_button;
    private RecyclerView recyclerView;
    private String train_name;
    private SearchAdapter adapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_search_train);
        initView();
        initEvent();

    }

    private void initView() {
        search_train = (EditText) findViewById(R.id.search_train);
        searchTrain_button = (Button) findViewById(R.id.searchTrain_button);
        recyclerView = (RecyclerView) findViewById(R.id.searchTrainRV);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));//设置布局


    }

    private void initEvent() {
        searchTrain_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String train_name = search_train.getText().toString();
                //  Log.d("A", "t_name");
                getData(train_name);
            }
            private void getData(String train_name) {
                TrainModel trainModel = new TrainModel();
                Call<List<Train>> call = trainModel.searchTrain(train_name);
                //System.out.print("t_name");
                call.enqueue(new Callback<List<Train>>() {
                    @Override
                    public void onResponse(Call<List<Train>> call, Response<List<Train>> response) {
                        List<Train> data = response.body();  //获取数据
                        // System.out.printf("data");
                        recyclerView.setAdapter(new TrainAdapter(SearchTrainActivity.this, data));
                    }

                    @Override
                    public void onFailure(Call<List<Train>> call, Throwable t) {

                    }
                });
            }
        });
    }

}

